package _电影院抢票案例;

import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 假设电影院有5排8座，一共四十张票
 * 电影院一共有30个窗口
 * 现在有100个person对象来购票，
 *
 */
public class Ticket extends Thread{
    private static List<String> ticketList =new ArrayList<>();
    private static List<Person> personList =new ArrayList<>();
    private static Random r = new Random();
    private static List<Person> successPersonList =new ArrayList<>();
    static {
        //四十张票
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 8; j++) {
                String seat=i+"排"+j+"座";
                ticketList.add(seat);
            }
        }

        //200个抢票人

        for (int i = 0; i < 200; i++) {
            personList.add(new Person(("黄牛"+i),false,null));
        }
    }
    @Override
    public void run() {
        synchronized ("锁"){
            //售卖40张票
            for (int i = 0; i < 40; i++) {
                int nextInt = r.nextInt(personList.size());
                Person person = personList.get(nextInt);
                person.setSeat(ticketList.get(i));
                person.setIsBuySuccess(true);
                successPersonList.add(personList.remove(nextInt));
            }
            System.out.println(successPersonList);
        }


    }

}
